package com.tanhua.app.service;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.UserLocationApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.NearUserVo;
import com.tanhua.domain.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    private UserLocationApi userLocationApi;
    @Reference
    private UserInfoApi userInfoApi;

    //上传用户经纬度
    public ResponseEntity saveLocation(Double x_longitude, Double y_latitude, String addrStr) {

        Long userId = UserHolder.getUser().getId();
        //将经纬度和地址以及当前的登录用户记录到user_location表中
        userLocationApi.saveLocation(x_longitude,y_latitude,addrStr,userId);
        return ResponseEntity.ok(null);
    }


    //搜附近
    public ResponseEntity search(String gender, Integer distance) {

        //1 根据当前用户的经纬度距离做周边用户的查询
        Long userId = UserHolder.getUser().getId();
        List<UserLocationVo> list = userLocationApi.search(userId, distance);
        //2 获取userInfo（头像 昵称）

        List<NearUserVo> nearUserVoList=new ArrayList();
        if(CollectionUtil.isNotEmpty(list)){
            for (UserLocationVo userLocationVo : list) {
                UserInfo userInfo = userInfoApi.findUserInfoVo(userLocationVo.getUserId());
                //1 排除自己
                if(userId==userInfo.getId()){
                    continue;
                }
                //2 排除性别
                if(!gender.equals(userInfo.getGender())){
                    continue;
                }
                //3 封装app需要的vo对象
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setUserId(userInfo.getId());
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());

                nearUserVoList.add(nearUserVo);
            }
        }
        return ResponseEntity.ok(nearUserVoList);
    }
}
